

#include "stdafx.h"
#include "EntityImp.h"
#include "EntityVisitorIf.h"
#include "Combat/CombatDefinition.h"
#include "Lua/LuaWrapper.h"
#include "EntityLua.h"


EntityImp::EntityImp(int nType /* = Entity::ett_Base */)
:m_wpStaticDataEx(NULL),
m_pCombatSys(NULL),
m_pMoveController(NULL),
m_pObs(NULL),
m_pInputDevice(NULL),
m_pOutputDevice(NULL),
m_nType(nType)
{
	
}
EntityImp::~EntityImp()
{

}

bool
EntityImp::onLoad()
{
	EntityLua::push(Lua::instance()->state(), this);
	return Lua::instance()->call("Entity", "onLoad", 1, 0);
}


void	EntityImp::setStaticID(int nID)
{
	m_nStaticID = nID;
}
int		EntityImp::getStaticID()const
{
	return m_nStaticID;
}

void	EntityImp::setID(int nID)
{
	m_nID = nID;
}
int		EntityImp::getID()const
{
	return m_nID;
}

void	EntityImp::setGlobalID(int nID)
{
	m_nGlobalID = nID;
}
int		EntityImp::getGlobalID()const
{
	return m_nGlobalID;
}


short	EntityImp::getType()const		{	return Entity::ett_Base;	}


void	EntityImp::setLevel(int v)
{
	m_nLvl = v;
}
int		EntityImp::getLevel()const	
{
	return m_nLvl;
}


void			EntityImp::setName(const string& n) 
{
	m_strName = n;
}
const string&	EntityImp::getName()const 
{
	return m_strName;
}


void	EntityImp::setPosition(const int& x, const int& y, const int& z)
{
	m_nPosX = x;
	m_nPosY = y;
	m_nPosZ = z;
}
void	EntityImp::getPosition(int& x, int& y, int& z)const
{
	x = m_nPosX;
	y = m_nPosY;
	z = m_nPosZ;
}

void	EntityImp::setDirection(const int& x, const int& y, const int& z)	
{
	m_nDirX = x;
	m_nDirY = y;
	m_nDirZ = z;
}
void	EntityImp::getDirection(int& x, int& y, int& z)const
{
	x = m_nDirX;
	y = m_nDirY;
	z = m_nDirZ;
}

void	EntityImp::setVisible(const int& v)
{
}

int		EntityImp::getVisible()const
{
	return 0;
}

void	EntityImp::setSpeedModifier(const int& v)	
{
	m_nSpeedModifier = v;
}
int		EntityImp::getSpeedModifier()const	
{
	return m_nSpeedModifier;
}

void	EntityImp::setGState(const short& st) 
{
	m_nGlobalState = st;
}
short	EntityImp::getGState()const
{
	return m_nGlobalState;
}

void	EntityImp::setMoveState(const short& st) 
{
	m_nMoveState = st;
}
short	EntityImp::getMoveState()const
{
	return m_nMoveState;
}

void	EntityImp::setMoveController(IMoveController* pMC)
{
	m_pMoveController = pMC;
}

IMoveController* EntityImp::getMoveController()
{
	return m_pMoveController;
}



void	EntityImp::setCombatState(const short& st)
{
	m_nCombatState = st;
}
short	EntityImp::getCombatState()const
{
	return m_nCombatState;
}

void	EntityImp::setCombatSys(ICombatSys* pCombat)
{
	if (m_pCombatSys)
	{
		delete m_pCombatSys;
	}

	if (pCombat)
	{
		pCombat->setHost(this);
	}

	m_pCombatSys = pCombat;
}

ICombatSys*	EntityImp::getCombatSys()
{
	return m_pCombatSys;
}

void	EntityImp::setFaction(short f)
{
	if (m_pCombatSys)
	{
		m_pCombatSys->setFaction(f);
	}
}

short	EntityImp::getFaction()const
{
	if (m_pCombatSys)
	{
		return m_pCombatSys->getFaction();
	}
	else
	{
		return Combat::FC_Unknown;
	}
}


void	EntityImp::setObserver(IObs* pObs)
{
	m_pObs = pObs;
}

IObs* EntityImp::getObserver()
{
	return m_pObs;
}

void	EntityImp::setInputDevice(IMsgListener* pMsgTgt)
{
	m_pInputDevice = pMsgTgt;
}

void	EntityImp::setOutputDevice(IMsgListener* pMsgTgt)
{
	m_pOutputDevice = pMsgTgt;
}

IMsgListener* EntityImp::getInputDevice()
{
	return	m_pInputDevice;
}

IMsgListener* EntityImp::getOutputDevice()
{
	return	m_pOutputDevice;
}


int		EntityImp::getProps(const short& nIdx, int d)const
{
	AttrMap::const_iterator it = m_attrs.find(nIdx);
	if(it != m_attrs.end())
	{
		return it->second;
	}
	return 0;
}

void	EntityImp::setProps(const short& nIdx, int v)
{
	m_attrs[nIdx] = v;
}

void	EntityImp::setFlg(const short& nIdx)
{
	if (nIdx>=0 && nIdx<=31)
	{
		m_nEntityFlg0_31 |= (1<<nIdx);
	}
}
void	EntityImp::clrFlg(const short& nIdx)	
{
	if (nIdx>=0 && nIdx<=31)
	{
		m_nEntityFlg0_31 &= ~(1<<nIdx);
	}
}
bool	EntityImp::hasFlg(const short& nIdx)	
{
	if (nIdx>=0 && nIdx<=31)
	{
		return ((m_nEntityFlg0_31) & (1<<nIdx)) != 0;
	}
	return false;
}

IDataEx*	EntityImp::getStaticEntityData()
{
	return m_wpStaticDataEx;
}
void		EntityImp::setStaticEntityData(IDataEx* pSED)
{
	m_wpStaticDataEx = pSED;
}

bool	EntityImp::accept(IEntityVisitor& v) 
{
	return v.visit(*this);
}

IScene*
EntityImp::getScene()
{
	return NULL;
}

bool
EntityImp::addGene(IGene*)
{
	return true;
}

bool
EntityImp::addGene(IGeneCreateData *pDynamicData)
{
	return true;
}

bool
EntityImp::rmvGene(int nGrpID)
{
	return true;
}

bool
EntityImp::addRegEvt(int nEvtGrp, int nEvt, IGene* pGene)
{
	return true;
}

bool
EntityImp::rmvRegEvt(int nEvtGrp, int nEvt, IGene* pGene)
{
	return true;
}

bool
EntityImp::onEvent(IEvt& evt)
{
	return true;
}


//
//		sub entities management
//

IEntityManager*
EntityImp::getEntityMgr()
{
	return NULL;
}
